package org.jetlinks.pro.tenant.utils;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.jetlinks.pro.PropertyConstants;
import org.jetlinks.pro.assets.AssetsUtils;

import java.util.*;

public class TenantUtils {

    @SuppressWarnings("all")
    public static Map<String, Object> createBindingKey(Map<String, Object> configs) {
        if (MapUtils.isEmpty(configs)) {
            return Collections.emptyMap();
        }
        List<Map<String, Object>> bindings = (List) configs.get(PropertyConstants.bindings.getKey());
        List<String> tenantId = (List) configs.get(PropertyConstants.tenantId.getKey());
        List<String> memberId = (List) configs.get(PropertyConstants.tenantMemberId.getKey());
        Map<String, Object> map = new LinkedHashMap<>();

        if (CollectionUtils.isNotEmpty(tenantId)) {
            map.put(PropertyConstants.tenantId.getKey(), tenantId);
        }
        if (CollectionUtils.isNotEmpty(memberId)) {
            map.put(PropertyConstants.tenantMemberId.getKey(), memberId);
        }
        if (CollectionUtils.isNotEmpty(bindings)) {
            map.put(PropertyConstants.bindings.getKey(), AssetsUtils.bindingsMapToKeys(bindings));
        }
        return map;
    }


}
